//! 数字生成器测试
//! 
//! 测试 src/generators/number.rs 中的数字生成功能

use dataforge::generators::number::*;

#[test]
fn test_adult_age_generation() {
    for _ in 0..100 {
        let age = adult_age();
        assert!(age >= 18);
        assert!(age <= 80); // 成年人年龄通常不超过80
        
        if age < 25 {
            println!("年轻成年人年龄: {}", age);
        } else if age > 60 {
            println!("老年人年龄: {}", age);
        }
    }
}

#[test]
fn test_child_age_generation() {
    for _ in 0..50 {
        let age = child_age();
        assert!(age >= 0);
        assert!(age < 18); // 儿童年龄小于18
        println!("儿童年龄: {}", age);
    }
}

#[test]
fn test_teenager_age_generation() {
    for _ in 0..50 {
        let age = teenager_age();
        assert!(age >= 13);
        assert!(age <= 19); // 青少年年龄13-19
        println!("青少年年龄: {}", age);
    }
}

#[test]
fn test_senior_age_generation() {
    for _ in 0..50 {
        let age = senior_age();
        assert!(age >= 60);
        assert!(age <= 100); // 老年人年龄60-100
        println!("老年人年龄: {}", age);
    }
}

#[test]
fn test_random_int_generation() {
    for _ in 0..100 {
        let num = random_int(1, 100);
        assert!(num >= 1);
        assert!(num <= 100);
    }
    
    // 测试边界情况
    let same_bounds = random_int(5, 5);
    assert_eq!(same_bounds, 5);
    
    let negative_range = random_int(-10, 10);
    assert!(negative_range >= -10);
    assert!(negative_range <= 10);
}

#[test]
fn test_random_float_generation() {
    for _ in 0..100 {
        let num = random_float(0.0, 1.0);
        assert!(num >= 0.0);
        assert!(num <= 1.0);
    }
    
    // 测试更大的范围
    for _ in 0..50 {
        let num = random_float(-100.0, 100.0);
        assert!(num >= -100.0);
        assert!(num <= 100.0);
    }
}

#[test]
fn test_percentage_generation() {
    for _ in 0..100 {
        let pct = percentage();
        assert!(pct >= 0.0);
        assert!(pct <= 100.0);
        
        if pct < 10.0 {
            println!("低百分比: {:.2}%", pct);
        } else if pct > 90.0 {
            println!("高百分比: {:.2}%", pct);
        }
    }
}

#[test]
fn test_score_generation() {
    for _ in 0..100 {
        let score = score();
        assert!(score >= 0);
        assert!(score <= 100);
        
        if score >= 90 {
            println!("优秀分数: {}", score);
        } else if score < 60 {
            println!("不及格分数: {}", score);
        }
    }
}

#[test]
fn test_price_generation() {
    for _ in 0..50 {
        let price = price();
        assert!(price >= 0.0);
        assert!(price <= 10000.0); // 价格通常不会太高
        
        // 价格应该有合理的精度（通常2位小数）
        let price_str = format!("{:.2}", price);
        let parsed_back: f64 = price_str.parse().unwrap();
        assert!((price - parsed_back).abs() < 0.01);
        
        println!("价格: ${:.2}", price);
    }
}

#[test]
fn test_salary_generation() {
    for _ in 0..50 {
        let salary = salary();
        assert!(salary >= 20000); // 最低工资
        assert!(salary <= 1000000); // 最高工资
        
        if salary > 100000 {
            println!("高薪: ${}", salary);
        } else if salary < 50000 {
            println!("低薪: ${}", salary);
        }
    }
}

#[test]
fn test_phone_number_generation() {
    for _ in 0..20 {
        let phone = phone_number();
        assert!(!phone.is_empty());
        
        // 电话号码应该只包含数字、空格、括号和连字符
        assert!(phone.chars().all(|c| c.is_ascii_digit() || 
                                     c == ' ' || c == '(' || c == ')' || 
                                     c == '-' || c == '+'));
        
        println!("电话号码: {}", phone);
    }
}

#[test]
fn test_credit_card_generation() {
    for _ in 0..10 {
        let card = credit_card();
        assert!(!card.is_empty());
        
        // 信用卡号应该只包含数字和空格/连字符
        let digits_only: String = card.chars().filter(|c| c.is_ascii_digit()).collect();
        assert!(digits_only.len() >= 13); // 信用卡号至少13位
        assert!(digits_only.len() <= 19); // 信用卡号最多19位
        
        println!("信用卡号: {}", card);
    }
}

#[test]
fn test_number_distribution() {
    let mut ages = Vec::new();
    let mut scores = Vec::new();
    
    for _ in 0..1000 {
        ages.push(adult_age());
        scores.push(score());
    }
    
    // 检查年龄分布
    let avg_age: f64 = ages.iter().map(|&x| x as u32).sum::<u32>() as f64 / ages.len() as f64;
    assert!(avg_age >= 30.0 && avg_age <= 60.0); // 平均年龄应该合理
    
    // 检查分数分布
    let avg_score: f64 = scores.iter().map(|&x| x as u32).sum::<u32>() as f64 / scores.len() as f64;
    assert!(avg_score >= 30.0 && avg_score <= 90.0); // 平均分数应该合理
    
    println!("平均年龄: {:.1}", avg_age);
    println!("平均分数: {:.1}", avg_score);
}

#[test]
fn test_number_uniqueness() {
    let mut phone_numbers = std::collections::HashSet::new();
    let mut credit_cards = std::collections::HashSet::new();
    
    for _ in 0..100 {
        phone_numbers.insert(phone_number());
        credit_cards.insert(credit_card());
    }
    
    // 电话号码和信用卡号应该有很高的唯一性
    assert!(phone_numbers.len() >= 95);
    assert!(credit_cards.len() >= 95);
}

#[test]
fn test_range_validation() {
    // 测试边界情况
    for _ in 0..50 {
        let min_val = random_int(0, 0);
        assert_eq!(min_val, 0);
        
        let max_val = random_int(100, 100);
        assert_eq!(max_val, 100);
        
        let float_val = random_float(5.5, 5.5);
        assert!((float_val - 5.5).abs() < 0.0001);
    }
}

#[test]
fn test_negative_numbers() {
    for _ in 0..50 {
        let neg_int = random_int(-100, -1);
        assert!(neg_int >= -100);
        assert!(neg_int <= -1);
        
        let neg_float = random_float(-10.0, -1.0);
        assert!(neg_float >= -10.0);
        assert!(neg_float <= -1.0);
    }
}